﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.Windows.Forms;
using ICSharpCode.TextEditor;
using ICSharpCode.TextEditor.Gui;
using DBSourceToolsLib;
using DBSourceToolsLib.Composite;
using DBSourceToolsLib.Suggestions;
using ICSharpCode.MSSQLExtensions.SqlParser;


namespace TestICSharpCode.MSSQLExtensions
{
    [TestFixture]
    public class TestSenseBuilder_Join
    {

        Form m_form = new Form();
        DBComposite m_DBComposite;

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            string strProjectFile = Constants.AdventureWorks_ProjectFile;
            DatabaseProject oDBProject = new DatabaseProject(strProjectFile);

            oDBProject.LoadFromDisk(false);
            Assert.AreEqual("AdventureWorks", oDBProject.ProjectName);
            Assert.Greater(oDBProject.SourceDatabases.Count, 0);
            Assert.AreEqual((string)"AdventureWorksLT", oDBProject.SourceDatabases[0].DatabaseName);
            Assert.AreEqual((string)"AdventureWorksLT", oDBProject.SourceDatabases[0].NickName);

            SourceDatabase oSource = oDBProject.SourceDatabases[0];

            System.Windows.Forms.Form oForm = new System.Windows.Forms.Form();

            SourceDatabaseLoader oSourceLoader =
                new SourceDatabaseLoader(oForm,
                    DBSourceToolsLib.Commands.DatabaseCommandTypes.LoadFromDisk);

            oSourceLoader.Execute(ref oSource, null);

            m_DBComposite = oSource.GetDBComposite();

        }

        [STAThread]
        [Test]
        public void TestSense_join_with_alias()
        {
            ICSharpCode.MSSQLExtensions.MSSQLTextEditor m_MsSqlTextEditor = new ICSharpCode.MSSQLExtensions.MSSQLTextEditor(m_form);

            SenseBuilder oSenseBuilder = new SenseBuilder(m_MsSqlTextEditor.Document);
            oSenseBuilder.SetComposite(m_DBComposite);

            string strText = 
"select *, from BuildVersion bv join ErrorLog er on ";

            m_MsSqlTextEditor.Text = strText;

            DateTime oDtStart = DateTime.Now;
            List<Suggestion> oSenseList = oSenseBuilder.GetSuggestionsForCursorAt(51);

            List<string> lstStrings = oSenseList.Select(sli => sli.Name).ToList();
            foreach (string strItem in lstStrings)
                Console.WriteLine(strItem);

            Assert.True(lstStrings.Contains("bv.ModifiedDate"));

        }

        [STAThread]
        [Test]
        public void TestSense_join()
        {
            ICSharpCode.MSSQLExtensions.MSSQLTextEditor m_MsSqlTextEditor = new ICSharpCode.MSSQLExtensions.MSSQLTextEditor(m_form);

            SenseBuilder oSenseBuilder = new SenseBuilder(m_MsSqlTextEditor.Document);
            oSenseBuilder.SetComposite(m_DBComposite);

            string strText =
"select *, from BuildVersion bv join ErrorLog er on er.id=bv.id join ";

            m_MsSqlTextEditor.Text = strText;

            DateTime oDtStart = DateTime.Now;
            List<Suggestion> oSenseList = oSenseBuilder.GetSuggestionsForCursorAt(68);

            List<string> lstStrings = oSenseList.Select(sli => sli.Name).ToList();
            foreach (string strItem in lstStrings)
                Console.WriteLine(strItem);

            Assert.True(lstStrings.Contains("ErrorLog"));

        }

        
        // TODO : select * from test as t join log E on
        [STAThread]
        [Test]
        public void TestSense_join_from_table_As()
        {
            ICSharpCode.MSSQLExtensions.MSSQLTextEditor m_MsSqlTextEditor = new ICSharpCode.MSSQLExtensions.MSSQLTextEditor(m_form);

            SenseBuilder oSenseBuilder = new SenseBuilder(m_MsSqlTextEditor.Document);
            oSenseBuilder.SetComposite(m_DBComposite);

            string strText =
"select *, from BuildVersion as bv ";

            m_MsSqlTextEditor.Text = strText;

            DateTime oDtStart = DateTime.Now;
            List<Suggestion> oSenseList = oSenseBuilder.GetSuggestionsForCursorAt(10);

            List<string> lstStrings = oSenseList.Select(sli => sli.Name).ToList();
            foreach (string strItem in lstStrings)
                Console.WriteLine(strItem);

            Assert.True(lstStrings.Contains("bv.ModifiedDate"));

        }



    }
}
